//JavaScript Framework 2.0 Code
try{
Type.registerNamespace('com.yonyou.ncmm_pac_pda.TransInScanController');
com.yonyou.ncmm_pac_pda.TransInScanController = function() {
    com.yonyou.ncmm_pac_pda.TransInScanController.initializeBase(this);
    this.initialize();
}
function com$yonyou$ncmm_pac_pda$TransInScanController$initialize(){
    //you can programing by $ctx API
    //get the context data through $ctx.get()
    //set the context data through $ctx.push(json)
    //set the field of the context through $ctx.put(fieldName, fieldValue)
    //get the parameter of the context through $ctx.param(parameterName)
    //Demo Code:
    //    var str = $ctx.getString();      //获取当前Context对应的字符串
    //    alert($ctx.getString())          //alert当前Context对应的字符串
    //    var json = $ctx.getJSONObject(); //获取当前Context，返回值为json
    //    json["x"] = "a";        //为当前json增加字段
    //    json["y"] = [];           //为当前json增加数组
    //    $ctx.push(json);            //设置context，并自动调用数据绑定
    //    
    //    put方法需手动调用databind()
    //    var x = $ctx.get("x");    //获取x字段值
    //    $ctx.put("x", "b");     //设置x字段值
    //    $ctx.put("x", "b");     //设置x字段值
    //    $ctx.databind();            //调用数据绑定才能将修改的字段绑定到控件上
    //    var p1 = $param.getString("p1");   //获取参数p2的值，返回一个字符串
    //    var p2 = $param.getJSONObject("p2");   //获取参数p3的值，返回一个JSON对象
    //    var p3 = $param.getJSONArray("p3");   //获取参数p1的值，返回一个数组
    
    //your initialize code below...
    
}
    
function com$yonyou$ncmm_pac_pda$TransInScanController$evaljs(js){
    eval(js)
}
//加载
function com$yonyou$ncmm_pac_pda$TransInScanController$onload(sender, args){
    var row = $param.getJSONObject("row");
    row = NCMMPubUtil.htmlspecialjson2UI(row);
    // 如果不存在数据表则先创建  
    NCMMPubUtil.createTable(TransInScanVO);
    if(!(row == null || row == "")) {
        var json = {};
        json = row;
        editVbatchcode=json.vbatchcode;
        $ctx.push(json);
        $ctx.dataBind();
    }
}
//下拉菜单
function opendropmenu(sender, args){
    TransInUtil.openDropDownList("btn_menu");
}
//手工录入条码或者pda红外扫码
function com$yonyou$ncmm_pac_pda$TransInScanController$inputMarCode(sender, args){
    marScanBack();
}
//扫物料码
function com$yonyou$ncmm_pac_pda$TransInScanController$doscan(sender, args){
    var listJson = $ctx.getJSONObject();
    if(NCMMPubUtil.isEmpty(listJson.cgeneralhid)) {
        $alert("请先指定单据！");
        return;
    }
    $scanner.open({
        bindfield: "marbarcode",
        callback: "marScanBack()"
    });
}
function marScanBack() {
    var marbarcode = $ctx.getString("marbarcode");
    if (marbarcode != null && marbarcode != "") {
        $id("textbox0").set("value", marbarcode);
        $ctx.dataBind();
    }
    parseMarCode(marbarcode);
    $id("textbox0").set("value", null);
    $id("textbox0").focus();
}
//解析条码
function parseMarCode(marbarcode) {
    var listJson = $ctx.getJSONObject();
    if(NCMMPubUtil.isEmpty(listJson.cgeneralhid)) {
        $alert("请先指定单据号！");
        return;
    }
    if(NCMMPubUtil.isEmpty(marbarcode)) {
        $alert("物料码为空！");
        return;
    }
    var marJson = NCMMBarcodeParseUtil.parseMaterialBar(marbarcode);
    var marID = marJson.pk_material;
    if(NCMMPubUtil.isEmpty(marID)) {
        $alert(marJson.failmsg);
        return;
    }
    if(!NCMMPubUtil.isEmpty(listJson.cgeneralbid)) {
        var wheresql = " where cgeneralbid='" + listJson.cgeneralbid + "'";
        NCMMPubUtil.queryVO(TransInVO, wheresql, function(data) {
            if(typeof(data) == 'undefined' || data == null || data.length == 0) {
                return;
            } else {
                var vos = eval(data);
                vos = NCMMPubUtil.htmlspecialjsonarray2UI(vos);
                if(vos != null && vos.length > 0) {
                    listJson = vos[0];
                }
            }
        });
    }
    if(NCMMPubUtil.isEmpty(listJson.cgeneralhid) || marID != listJson.cmaterialvid || marJson.vbatchcode != listJson["vbatchcode"]) {
        var vo = queryScanInfo(marID, marJson.vbatchcode, listJson.cgeneralhid);
        if(NCMMPubUtil.isEmpty(vo)) {
            $alert("扫描物料" + marJson.code + "不在对应的单据" + listJson["vbillcode"] + "中");
            return;
        }
        listJson = vo;
    }
    //扫描的批次号与单据本身的批次号均不空且不同为true
    var isNotEditAndBatchcodeDif=NCMMPubUtil.isNotEmpty(listJson["vbatchcode"]) && NCMMPubUtil.isNotEmpty(marJson.vbatchcode) && listJson["vbatchcode"] != marJson.vbatchcode;
    if(isNotEditAndBatchcodeDif) {
        $alert("扫描物料" + marJson.code + "的批次号和单据" + listJson["vbillcode"] + "的批次号不符！");
        return;
    }
    if(NCMMPubUtil.isNotEmpty(marJson.vbatchcode)) {
        listJson["vbatchcode"] = marJson.vbatchcode;
        listJson["pk_batchcode"] = marJson.pk_batch;
    }
    listJson["vserialcode"] = marJson.vserialcode;
    var num = 1;
    var vchangerate = listJson["vchangerate"];
    var nastnum = TransInUtil.getAstnum(1, vchangerate);
    var unitBitNum = TransInUtil.getUnitBitnumber(listJson["cunitid"]);
    if(!NCMMPubUtil.isEmpty(unitBitNum)) {
        num = num.toFixed(unitBitNum);
    }
    var astunitBitNum = TransInUtil.getUnitBitnumber(listJson["castunitid"]);    
    if(!NCMMPubUtil.isEmpty(astunitBitNum)) {
     nastnum =TransInUtil.decimalPrecision(nastnum,astunitBitNum);
    }
    listJson["nscannum"] = num;
    listJson["nscanastnum"] = nastnum;
    listJson["pk_transinscan"] = getTransScanInPK();
    NCMMPubUtil.insertVO(TransInScanVO, listJson);
    TransInUtil.updateTransInScanedNum(listJson.cgeneralbid);
    $ctx.push(listJson);
    $ctx.dataBind();
}
function getTransScanInPK() {
    var date = new Date().getTime();
    var pk = "4007" + date.toString() + "222";
    return pk;
}
//查询扫描信息
function queryScanInfo(marID, vbatchcode, cgeneralhid) {
    var vo = {};
    var whereSql = " where cgeneralhid='" + cgeneralhid + "' and cmaterialvid='" + marID + "'";
    NCMMPubUtil.queryVO(TransInVO, whereSql, function(data) {
        if(typeof(data) == 'undefined' || data == null || data.length == 0) {
            vo = null;
        } else {
            var vos = eval(data);
            vos = NCMMPubUtil.htmlspecialjsonarray2UI(vos);
            if(vos != null && vos.length > 0) {
                if(vos.length == 1) {
                    vo = vos[0];
                } else {
                    vo = getScanVO(vos, vbatchcode);
                }
            } else {
                vo = null;
            }
        }
    });
    return vo;
}
function com$yonyou$ncmm_pac_pda$TransInScanController$close(sender, args){
    $view.close();
}
//修改辅数量时计算主数量
function com$yonyou$ncmm_pac_pda$TransInScanController$calNum(sender, args){
    var nn = args["newvalue"];
    if(NCMMPubUtil.isEmpty(nn)) {
        $ctx.put("nscannum", "");
        $ctx.dataBind();
        return;
    }
    if(nn == "-") {
        return;
    }
    if(!$.isNumeric(nn)) {
        $ctx.put("nscanastnum", args["oldvalue"]);
        $ctx.dataBind();
        return;
    }
    if(TransInUtil.isBsBitnumber(nn, $ctx.getString("castunitid"))) {
        $ctx.put("nscanastnum", args["oldvalue"]);
        $ctx.dataBind();
        return;
    }
    $ctx.dataCollect();
    var nastnum = $ctx.getString("nscanastnum");
    if(NCMMPubUtil.isEmpty(nastnum)){
        //辅数量输入为空
        $ctx.put("nscannum", "");
        $ctx.dataBind();
        return;
    }
    if(!$.isNumeric(nastnum)) {
        $alert("扫描数量输入不合法！");
        return;
    }
    var vchangerate = $ctx.getString("vchangerate");
    var nnum;
    if(!NCMMPubUtil.isEmpty(nastnum)) {
        nnum = TransInUtil.getNum(nastnum, vchangerate);
        var bitnum = TransInUtil.getUnitBitnumber($ctx.getString("cunitid"));
        if(!NCMMPubUtil.isEmpty(bitnum)) {
            nnum = TransInUtil.decimalPrecision(nnum,bitnum);
        }
        if(nnum.toString() == "NaN") {
            $alert("数量输入不合法！");
            return;
        }
    }
    $ctx.put("nscannum", nnum);
    $ctx.dataBind();
}
//修改主数量时计算辅数量
function com$yonyou$ncmm_pac_pda$TransInScanController$calAstNum(sender, args){
    var nn = args["newvalue"];
    if(NCMMPubUtil.isEmpty(nn)) {
        $ctx.put("nscanastnum", "");
        $ctx.dataBind();
        return;
    }
    if(nn == "-") {
        return;
    }
    if(!$.isNumeric(nn)) {
        $ctx.put("nscannum", args["oldvalue"]);
        $ctx.dataBind();
        return;
    }
    if(TransInUtil.isBsBitnumber(nn, $ctx.getString("cunitid"))) {
        $ctx.put("nscannum", args["oldvalue"]);
        $ctx.dataBind();
        return;
    }
    $ctx.dataCollect();
    var nnum = $ctx.getString("nscannum");
    if(NCMMPubUtil.isEmpty(nnum)) {
        //主数量输入为空
        $ctx.put("nscanastnum", "");
        $ctx.dataBind();
        return;
    }
    if(!$.isNumeric(nnum)) {
        $alert("扫描主数量输入不合法！");
        return;
    }
    var vchangerate = $ctx.getString("vchangerate");
    var nastnum = "";
    if(!NCMMPubUtil.isEmpty(nnum)) {
        nastnum = TransInUtil.getAstnum(nnum, vchangerate);
        var bitnum = TransInUtil.getUnitBitnumber($ctx.getString("castunitid"));
        if(!NCMMPubUtil.isEmpty(bitnum)) {
            nastnum =TransInUtil.decimalPrecision(nastnum,bitnum);
        }
        if(nastnum.toString() == "NaN") {
            $alert("数量输入不合法！");
            return;
        }
    }
    $ctx.put("nscanastnum", nastnum);
    $ctx.dataBind();
}
//保存
function com$yonyou$ncmm_pac_pda$TransInScanController$saveScanInfo(sender, args){
    var json = $ctx.getJSONObject();
    if(json.nscannum == null || json.nscannum == 0 || json.nscannum == "0") {
        $alert("扫描主数量不能为空或0！");
        return;
    }
    if(json.nscanastnum == null || json.nscanastnum == 0 || json.nscanastnum == "0") {
        $alert("扫描数量不能为空或0！");
        return;
    }
    if(!$.isNumeric(json.nscannum) || !$.isNumeric(json.nscanastnum)) {
        $alert("数量输入不合法！");
        return;
    }
    json=NCMMPubUtil.htmlspecialjson2UI(json);
    var isSuccess = doSave(json);
    if(isSuccess == true) {
        $toast("保存成功！");
    }
}
//保存按钮保存或提交前保存
function doSave(json) {
    if (NCMMPubUtil.isEmpty(json["vbatchcode"])) {
        json["pk_batchcode"] = "";
    } else {
        var sql = " where vbatchcode='" + json["vbatchcode"] + "' and cmaterialvid='" + json["cmaterialvid"] + "'";
        var pk_batchcode = "";
        if(NCMMPubUtil.isTableExist(BatchcodeVO) ){
            NCMMPubUtil.queryVO(BatchcodeVO, sql, function(data) {
                if(typeof(data) == 'undefined' || data == null || data.length == 0) {
                    return;
                } else {
                    var vos = eval(data);
                    if(vos != null && vos.length > 0) {
                        pk_batchcode = vos[0].pk_batchcode;
                    }
                }
            });
        }
        if(NCMMPubUtil.isEmpty(pk_batchcode)) {
            json["pk_batchcode"] = "";
            $alert("批次号不在批次档案中，请检查输入或更新批次档案！");
            return false;
        }
        json["pk_batchcode"] = pk_batchcode;
    }
    var billBatchCode = getBillBatchCode(json["cgeneralbid"]);
    if(NCMMPubUtil.isEmpty(json["pk_transinscan"])) {
        if(!NCMMPubUtil.isEmpty(json["vbatchcode"]) && !NCMMPubUtil.isEmpty(billBatchCode) && json["vbatchcode"] != billBatchCode) {
            $alert("扫描物料" + json["cmaterialvid_name"] + "的批次号和单据" + json["vbillcode"] + "的批次号不符！");
            return false;
        }
        json["pk_transinscan"] = getTransScanInPK();
        NCMMPubUtil.insertVO(TransInScanVO, json);
        $ctx.push(json);
        $ctx.dataBind();
    } else {
        var oldBatchCode = getScanBatchCode(json["pk_transinscan"]);
        if(!NCMMPubUtil.isEmpty(json["vbatchcode"]) && !NCMMPubUtil.isEmpty(billBatchCode) && json["vbatchcode"] != oldBatchCode && json["vbatchcode"] != billBatchCode) {
            $alert("扫描物料" + json["cmaterialvid_name"] + "的批次号和单据" + json["vbillcode"] + "的批次号不符！");
            return false;
        }
        NCMMPubUtil.updateVO(TransInScanVO, json);
        $ctx.push(json);
        $ctx.dataBind();
    }
    //更新列表扫描数量 
    TransInUtil.updateTransInScanedNum(json.cgeneralbid);
    return true;
}
//查询记录内批次号
function getScanBatchCode(pk_transinscan) {
    var sql = " where pk_transinscan='" + pk_transinscan + "'";
    var batchcode = "";
    NCMMPubUtil.queryVO(TransInScanVO, sql, function(data) {
        if(typeof(data) == 'undefined' || data == null || data.length == 0) {
            return;
        } else {
            var vos = eval(data);
            vos = NCMMPubUtil.htmlspecialjsonarray2UI(vos);
            if(vos != null && vos.length > 0) {
                batchcode = vos[0].vbatchcode;
            }
        }
    });
    return batchcode;
}
//查询批次号
function getBillBatchCode(cgeneralbid) {
    var sql = " where cgeneralbid='" + cgeneralbid + "'";
    var batchcode = "";
    NCMMPubUtil.queryVO(TransInVO, sql, function(data) {
        if(typeof(data) == 'undefined' || data == null || data.length == 0) {
            return;
        } else {
            var vos = eval(data);
            vos = NCMMPubUtil.htmlspecialjsonarray2UI(vos);
            if(vos != null && vos.length > 0) {
                batchcode = vos[0].vbatchcode;
            }
        }
    });
    return batchcode;
}
function com$yonyou$ncmm_pac_pda$TransInScanController$doCommit(sender, args){
var json = $ctx.getJSONObject();
		var stordoc = $cache.read("defaultstordoc");
		//默认仓库和调入仓库均为空
        if(NCMMPubUtil.isEmpty(stordoc)&&NCMMPubUtil.isEmpty(json.cotherwhid)) {
            $alert("默认仓库不能为空，请设置！");
            return;
        }
		if(NCMMPubUtil.isEmpty(json.cgeneralbid)) {
			$alert("无扫描数据，不能提交！");
			return;
		}
		if(json.nscannum == null || json.nscannum == 0 || json.nscannum == "0") {
			$alert("扫描主数量不能为空或0！")
			return;
		}
		if(json.nscanastnum == null || json.nscanastnum == 0 || json.nscanastnum == "0") {
			$alert("扫描数量不能为空或0！")
			return;
		}
		if(!$.isNumeric(json.nscannum) || !$.isNumeric(json.nscanastnum)) {
			$alert("数量输入不合法！")
			return;
		}
		json=NCMMPubUtil.htmlspecialjson2UI(json);
		var isSuccess = doSave(json);
		if(isSuccess == false) {
			return;
		}
		var scanNum = TransInUtil.getScanVOCount(json.cgeneralbid);
		if(scanNum > 1) {
			$js.hideLoadingBar();
			$alert("当前单据行有其他扫描记录，请全部选中后再提交！");
			return;
		}
		var sql = " where pk_transinscan='" + json.pk_transinscan + "'";
		var vo = {};
		NCMMPubUtil.queryVO(TransInScanVO, sql, function(scanData) {
			if(typeof(scanData) == 'undefined' || scanData == null || scanData.length == 0) {
				return;
			}
			var vos = eval(scanData);
			  vos = NCMMPubUtil.htmlspecialjsonarray2UI(vos);
			vo = vos[0];
		});
		if(NCMMPubUtil.isEmpty(vo)) {
			$alert("当前扫描记录已经被删除，不能提交！")
			return;
		}
		//组件提交billinfos 
		var head = {};
		head["csourcebillhid"] = vo.cgeneralhid;
		//如果默认仓库不为空取默认仓库，否则取单据上的调入仓库
		if(NCMMPubUtil.isEmpty(stordoc)||NCMMPubUtil.isEmpty($stringToJSON(stordoc).pk_stordoc)){
			head["cwarehouseid"] =vo.cotherwhid;
		}
		else{
			head["cwarehouseid"] = $stringToJSON(stordoc).pk_stordoc;
		}
		head["ts_h"] = vo.hts;
		var item = {};
		item["csourcebillbid"] = vo.cgeneralbid;
		item["cmaterialvid"] = vo.cmaterialvid;
		item["castunitid"] = vo.castunitid;
		item["nscannum"] = vo.nscannum;
		item["nscanastnum"] = vo.nscanastnum;
		item["pk_batchcode"] = vo.pk_batchcode;
		item["vbatchcode"] = vo.vbatchcode;
		item["ts_b"] = vo.bts;	
		if(!NCMMPubUtil.isEmpty(vo.vserialcode) || !NCMMPubUtil.isEmpty(vo.cinspaceid)) {
			var bbitem = {};
			//bbitem["pk_serialcode"] = vo.pk_serialcode;
			bbitem["vserialcode"] = vo.vserialcode;
			bbitem["nnum"] = vo.nscannum;
			bbitem["nassistnum"] = vo.nscanastnum;
			bbitem["clocationid"] = vo.cinspaceid;
			var bbitems = [{
				"bbitem": bbitem
			}];
			item["bbitems"] = bbitems;
		}
		var items = [{
			"item": item
		}];
		var billinfo = {
			"head": head,
			"items": items
		};
		var billinfos = [{
			"billinfo": billinfo
		}];			
		var isSuccess = TransInUtil.commitData(billinfos,json.pk_transinscan,function(data){		 
		   //更新提交数量 以及扫描数量 扫描数量去除提交数量		  		 
		  //TransInUtil.updateBillAfterCommit(vo.cgeneralbid,vo.nscannum,vo.nscanastnum);
		    //清空界面值
           doClear();		  		   
		});
	
}
function doClear(){
	   var list = {};
		$ctx.push(list);
		$ctx.dataBind();
	}
function onEventClick_showSpaceRef(sender, args){
var stordoc = $cache.read("defaultstockorg");
        if(stordoc == null || stordoc == "") {
            $alert("默认仓库不能为空，请设置！");
            return;
        }
        openRefPage("PDARACK", "cinspaceid_name", "cinspaceid", "pk_rack");

}
com.yonyou.ncmm_pac_pda.TransInScanController.prototype = {
    doCommit : com$yonyou$ncmm_pac_pda$TransInScanController$doCommit,
    saveScanInfo : com$yonyou$ncmm_pac_pda$TransInScanController$saveScanInfo,
    calAstNum : com$yonyou$ncmm_pac_pda$TransInScanController$calAstNum,
    calNum : com$yonyou$ncmm_pac_pda$TransInScanController$calNum,
    close : com$yonyou$ncmm_pac_pda$TransInScanController$close,
    doscan : com$yonyou$ncmm_pac_pda$TransInScanController$doscan,
    inputMarCode : com$yonyou$ncmm_pac_pda$TransInScanController$inputMarCode,
    onload : com$yonyou$ncmm_pac_pda$TransInScanController$onload,
    initialize : com$yonyou$ncmm_pac_pda$TransInScanController$initialize,
    evaljs : com$yonyou$ncmm_pac_pda$TransInScanController$evaljs
};
com.yonyou.ncmm_pac_pda.TransInScanController.registerClass('com.yonyou.ncmm_pac_pda.TransInScanController',UMP.UI.Mvc.Controller);
}catch(e){$e(e);}
